﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;


namespace SPG_Scrollable.ScrollableSVWP
{
    [ToolboxItem(false)]
    public partial class ScrollableSVWP : System.Web.UI.WebControls.WebParts.WebPart
    {

        #region set WebPart defaults
        private const Int32 rowLimitDefault = 15;
        public Int32 rowLimit = rowLimitDefault;
        [WebBrowsable(true),
           Category("Configuration"),
           Personalizable(PersonalizationScope.User),
           DefaultValue(rowLimitDefault),
           WebDisplayName("Row Limit"),
           WebDescription("enter the number of items to be returned by the spquery")]
        public Int32 RowLimit
        {
            get { return rowLimit; }
            set { rowLimit = value; }
        }

        private const Int32 thumbnailDisplayDefault = 3;
        public Int32 thumbnailDisplayCount = thumbnailDisplayDefault;
        [WebBrowsable(true),
        Category("Configuration"),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(thumbnailDisplayDefault),
        WebDisplayName("Display Thumbnails"),
        WebDescription("this is the number of thumbnails to display in the control")]
        public Int32 ThumbnailDisplayCount
        {
            get { return thumbnailDisplayCount; }
            set { thumbnailDisplayCount = value; }
        }


        private const string WebURLDefault = "/";
        public string webURL = WebURLDefault;
        [WebBrowsable(true),
        Category("Configuration"),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue(WebURLDefault),
        WebDisplayName("Web URL"),
        WebDescription("enter the relative URL for the site, for example the home site is '/'")]
        public string WebURL
        {
            get { return webURL; }
            set { webURL = value; }
        }

        public string photoGallery;
        [WebBrowsable(true),
        Category("Configuration"),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue("PhotoGallery"),
        WebDisplayName("Photo Gallery"),
        WebDescription("enter the name of the photo gallery list to display images")]
        public string PhotoGallery
        {
            get { return photoGallery; }
            set { photoGallery = value; }
        }
        public string camlQuery;
        [WebBrowsable(true),
        Category("Configuration"),
        Personalizable(PersonalizationScope.Shared),
        DefaultValue("/"),
        WebDisplayName("CAML Query"),
        WebDescription("enter the CAML query for the spquery object. leave out the <query> tags")]
        public string CamlQuery
        {
            get { return camlQuery; }
            set { camlQuery = value; }
        }
        #endregion

        SPListItemCollection myPhotos;
        
        
        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
        protected override void Render(HtmlTextWriter writer)
        {
            if (webURL != null && photoGallery != null && thumbnailDisplayCount != 0)
            {
                myPhotos = getSPList(camlQuery, webURL, photoGallery, rowLimit);

                writeScrollableWrapperTop(writer);
                writeScrollableListItems(writer,
                    thumbnailDisplayCount,
                    myPhotos);
                writeScrollableWrapperBottom(writer);
                writeScrollableListItemDetails(writer,
                    myPhotos);

                
            }
            
            
        }       

        
        protected void writeScrollableWrapperTop(HtmlTextWriter customWriter)
        {
            
            customWriter.Write("<div class='spotlightcontainer'>");
            customWriter.Write("<!-- wrapper element for the large image -->");
            customWriter.Write("<div id='image_wrap'>");
         
            customWriter.Write("<div class='image_wrap_img'>");
            customWriter.Write("<!-- Initially the image is a simple 1x1 pixel transparent GIF -->");
            customWriter.Write("<img src='/Resources%20%20Images/blank.gif' />");
            customWriter.Write("</div>");
            customWriter.Write("<div class='image_wrap_details'>");
            //customWriter.Write("<h3>h3 tag here</h3>");
            customWriter.Write("<strong>strong text here</strong>");
            customWriter.Write("<p>first paragraph here</p>");
            customWriter.Write("<a href=''>no DisplayHyperlink text value found!</a>");
            customWriter.Write("</div>");
            customWriter.Write("</div>");

            customWriter.Write("<!-- HTML structures -->");
            
            customWriter.Write("<div class='thumbnailcontainer'>");

            customWriter.Write("<!-- 'previous page' action --> ");
            customWriter.Write("<a class='prev browse left'></a>");
            customWriter.Write(" <!-- root element for scrollable --> ");
            customWriter.Write("<div class='scrollable'> ");

            customWriter.Write("<!-- root element for the items --> ");
            customWriter.Write("<div id='itemsDiv' runat='server' class='items'> ");


        }
        protected void writeScrollableWrapperBottom(HtmlTextWriter customWriter)
        {
            customWriter.Write("</div>");
            customWriter.Write("</div>");
            customWriter.Write("<!-- 'next page' action --> ");
            customWriter.Write("<a class='next browse right'></a>");
            customWriter.Write("</div>");

            
        
            customWriter.Write("</div>");
            //this last closing div should close up the thumbnailcontainer
            customWriter.Write("</div>");
            //this last closing div should close up the spotlightcontainer

        }

        protected void writeScrollableListItems(HtmlTextWriter customWriter,
            int thumbnailDisplayNbr,
            SPListItemCollection collListItems)
        {
            try
            {
              
                int counter = 0;
                string strImage;
                string strAlt;
           

                //SPListItemCollection collListItems = getSPList(camlQuery, webURL, listName, rowLimit);

                foreach (SPListItem oListItem in collListItems)
                {
                    if (counter == 0)
                    {
                        //this is where I want to open the div tag
                        //write out <div>
                        customWriter.Write("<div>");
                    }

                    //this is where I want to display the image
                    strImage = oListItem.Url;
                    strAlt = oListItem.Name;
                    strImage = "<img src='/" + strImage + "'" + "alt='" + strAlt + "' />";
                    //write out the image here

                    customWriter.Write(strImage);
                    customWriter.Write("<div id=\"itemdetailthumbnail\" >");
                    customWriter.Write("Posted " + String.Format("{0:MM/dd/yyyy}", oListItem.File.TimeCreated));
                    customWriter.Write("<br/>");
                    customWriter.Write("By " + oListItem.File.Author.ToString());

                    customWriter.Write("</div>");

                    if (counter == thumbnailDisplayNbr)
                    {
                        //this is where I want to close the div tag
                        //write out </div>

                        customWriter.Write("</div>");

                        //duh set the counter to zero here or since it is getting increment below set it to -1
                        counter = -1;
                    }
                    else if (counter == collListItems.Count)
                    {
                        //lets close the div tags here because we are done
                        //write out </div>
                        customWriter.Write("</div>");
                    }
                    //now increment
                    counter = counter + 1;
                }
            }
            catch (Exception ex)
            {
                customWriter.Write("one of the parameters were invalid. fix it! " + ex.Message);
            }



        }
        protected void writeScrollableListItemDetails(HtmlTextWriter customWriter,            
            SPListItemCollection collListItems)
        {
            //what I need is this:
            //<div>
            //<div class = item> //lets add in an id to each div based on the name of the individual item so the js can look it up?
            //img
            //h3
            //strong
            //p
            //<a href="url">Link text</a> (read more hyperlink)
            //</div>
            //</div>
            string strImage;

            customWriter.Write("<div class='itemshiddencontainer'>");            

            foreach (SPListItem oListItem in collListItems)
            {
                customWriter.Write("<div class = 'itemshidden' id='" + oListItem.Name.Substring(0, oListItem.Name.IndexOf('.')) + "'>");
                strImage = oListItem.Url;
                strImage = "<img src='/" + strImage + "'" + "alt='" + oListItem.Name + "' />";
                customWriter.Write(strImage);

                
                if (verifyFields("StrongText", oListItem))
                {
                    customWriter.Write("<strong>" + oListItem["StrongText"].ToString() + "</strong>");
                }
                else
                {
                    customWriter.Write("<strong>no strong text value found!</strong>");
                }
                if (verifyFields("ParagraphText", oListItem))
                {
                    customWriter.Write("<p>" + oListItem["ParagraphText"].ToString() + "</p>");
                }
                else
                {
                    customWriter.Write("<p>no paragraph text value found!</p>");
                }
                if (verifyFields("DisplayHyperlink", oListItem))
                {
                    customWriter.Write("<a href=" + oListItem["DisplayHyperlink"].ToString() + ">For more info click here!</a>");
                }
                else
                {
                    customWriter.Write("<a href=''>no DisplayHyperlink text value found!</a>");
                }
                customWriter.Write("</div>");

            }
            customWriter.Write("</div>");


        }



        protected bool verifyFields(string columnname, SPListItem oListItem)
        {
            bool verify = false;

            if (oListItem.Fields.ContainsField(columnname) && oListItem[columnname] != null)
            {
                verify = true;
            }

            return verify;

        }

        

        


        protected SPListItemCollection getSPList(string camlQuery, string webURL, string listName, int rowLimit)
        {

            SPSite oSiteCollection = SPContext.Current.Site;

            SPList oList = oSiteCollection.AllWebs[webURL].Lists[listName];
            SPQuery oQuery = new SPQuery();
            //need to do caml query here if I wanted
            //oQuery.Query = "<Where><Eq><FieldRef Name='Publish'/>" +
            //"<Value Type='Choice'>Yes</Value></Eq></Where>";
            oQuery.Query = camlQuery;
            oQuery.RowLimit = Convert.ToUInt16(rowLimit);
            SPListItemCollection collListItems = oList.GetItems(oQuery);

            return collListItems;
        }

    }
}
